Cartoonize your images using CartoonGAN, powered by TensorFlow 2.0.
This repo contain the Python script that we will use to generate cartoon-style images.
import os
repo = "CartoonGan-tensorflow"
!git clone https://github.com/mnicnc404/{repo}.git
os.chdir(os.path.join(repo))
!ls | grep cartoonize.py
from IPython.display import clear_output, display, Image
!git clone https://www.github.com/keras-team/keras-contrib.git \
&& cd keras-contrib \
&& python convert_to_tf_keras.py \
&& USE_TF_KERAS=1 python setup.py install
clear_output()
import tensorflow as tf
Download new images to cartoonize
image_url = 'https://media.giphy.com/media/EcrGURaK4c7yZlZkzf/giphy.gif' #@param {type: "string"}
input_image_dir = "input_images"
output_image_dir = input_image_dir.replace("input_", "output_")
import time
if image_url:
img_filename = image_url.split("/")[-1]
name, ext = '.'.join(img_filename.split(".")[:-1]), img_filename.split(".")[-1]
new_name = '_'.join((name, str(int(time.time()))))
new_img_filename = '.'.join((new_name, ext))
image_path = os.path.join(input_image_dir, new_img_filename)
!wget {image_url} \
&& mv {img_filename} {new_img_filename} \
&& mv {new_img_filename} {image_path}
# a trick to show gif in notebook
if ".gif" in new_img_filename:
png_path = new_img_filename + '.png'
!cp {image_path} {png_path}
display(Image(png_path))
Transform the downloaded image using selected style.
styles = "hosoda" #@param ["shinkai", "hayao", "hosoda", "paprika"]
!python cartoonize.py \
--styles {styles} \
--batch_size 1 \
--comparison_view horizontal
if img_filename:
if ".gif" in img_filename:
generated_gif = os.path.join(output_image_dir, "comparison", new_img_filename)
result_path = generated_gif + '.png'
!cp {generated_gif} {result_path}
else:
result_path = os.path.join(output_image_dir, "comparison", new_img_filename)
display(Image(result_path))